package g11.actionutil;

import g11.dao.AppointmentDAO;
import g11.dao.DAOException;
import g11.dao.VisitationDAO;
import g11.model.Appointment;
import g11.model.Employee;
import g11.model.Patient;
import g11.model.Visitation;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

public class VisitationUtilImpl implements VisitationUtil {

	@Override
	public List<Visitation> getVisitationByDoctor(Employee doctor)
			throws Exception {
		AppointmentDAO apDao = new g11.dao.AppointmentDAOImpl();
		VisitationDAO vtDao = new g11.dao.VisitationDAOImpl();
		List<Appointment> appointmentList = new ArrayList<Appointment>();
		List<Visitation> visitationList = new ArrayList<Visitation>();

		try {
			appointmentList = apDao.getAllVisitedAppointmentByDoctor(doctor);

			for (Appointment app : appointmentList) {
				Visitation visitation = vtDao.getCurrentVersionOfVisitation(app
						.getAppointmentId());
				visitationList.add(visitation);
			}
		} catch (DAOException e) {
			throw new DAOException(e.getMessage());
		}

		return visitationList;
	}

	@Override
	public List<Visitation> getVisitationByPatient(Patient patient)
			throws Exception {
		AppointmentDAO apDao = new g11.dao.AppointmentDAOImpl();
		VisitationDAO vtDao = new g11.dao.VisitationDAOImpl();
		List<Appointment> appointmentList = new ArrayList<Appointment>();
		List<Visitation> visitationList = new ArrayList<Visitation>();

		try {
			appointmentList = apDao.getAllVisitedAppointmentByPatient(patient);

			for (Appointment app : appointmentList) {
				Visitation visitation = vtDao.getCurrentVersionOfVisitation(app
						.getAppointmentId());
				visitationList.add(visitation);
			}
		} catch (DAOException e) {
			throw new DAOException(e.getMessage());
		}

		return visitationList;
	}

	@Override
	public List<Visitation> getVisitationByDoctorAndDate(Employee doctor,
			Date date) throws Exception {

		AppointmentDAO apDao = new g11.dao.AppointmentDAOImpl();
		VisitationDAO vtDao = new g11.dao.VisitationDAOImpl();
		List<Appointment> appointmentList = new ArrayList<Appointment>();
		List<Visitation> visitationList = new ArrayList<Visitation>();
		appointmentList = apDao.getPendingAppointmentByDoctor(doctor);
		int i = 0;
		while (i < appointmentList.size()) {
			byte apStatus = appointmentList.get(i).getStatus();
			Date date1 = appointmentList.get(i).getAppointmentDate();
			if (apStatus == 0 && date1 == date) {
				Visitation visitation = new g11.model.Visitation();
				visitation = vtDao
						.getCurrentVersionOfVisitation(appointmentList.get(i)
								.getAppointmentId());
				visitationList.add(visitation);
			}
			i++;
		}

		return visitationList;
	}

	@Override
	public List<Visitation> getVisitationByPatientAndDate(Patient patient,
			Date date) throws Exception {

		AppointmentDAO apDao = new g11.dao.AppointmentDAOImpl();
		VisitationDAO vtDao = new g11.dao.VisitationDAOImpl();
		List<Appointment> appointmentList = new ArrayList<Appointment>();
		List<Visitation> visitationList = new ArrayList<Visitation>();
		appointmentList = apDao.getAppointmentByPatient(patient);
		int i = 0;
		while (i < appointmentList.size()) {
			byte apStatus = appointmentList.get(i).getStatus();
			Date date1 = appointmentList.get(i).getAppointmentDate();
			if (apStatus == 0 && date1 == date) {
				Visitation visitation = new g11.model.Visitation();
				visitation = vtDao
						.getCurrentVersionOfVisitation(appointmentList.get(i)
								.getAppointmentId());
				visitationList.add(visitation);
			}
			i++;
		}

		return visitationList;
	}

}
